package com.zqw.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqw.info.ResponseMsg;
import com.zqw.pojo.AdminPojo;
import com.zqw.pojo.GoodsPojo;
import com.zqw.pojo.GoodsTypePojo;
import com.zqw.service.GoodsService;
import com.zqw.util.StringUtil;
import com.zqw.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    //查询所有的商品类型
    @RequestMapping("typeList")
    public String typeList(HttpServletRequest request){
        List<GoodsTypePojo> goodsTypePojos = goodsService.queryGoodsTypes(new GoodsTypePojo());
        if(goodsTypePojos != null){
            request.setAttribute("list",goodsTypePojos);
            return "type-list";
        }else{
            return "error";
        }
    }

    //更改类型的状态
    @RequestMapping("updTypeStatus")
    @ResponseBody
    public String updTypeStatus(GoodsTypePojo goodsTypePojo){
        boolean b = goodsService.updGoodsType(goodsTypePojo);
        if(b){
            return "0000";
        }else{
            return "0001";
        }
    }

    //跳转到更改类型页面
    @RequestMapping("typeEdit")
    public String typeEdit(GoodsTypePojo goodsTypePojo,HttpServletRequest request){
        List<GoodsTypePojo> goodsTypePojos = goodsService.queryGoodsTypes(goodsTypePojo);
        if(goodsTypePojos!=null){
            request.setAttribute("goodsType",goodsTypePojos.get(0));
            return "type-edit";
        }else{
            return "error";
        }
    }

    //类型名唯一
    @RequestMapping("soleGoodsType")
    @ResponseBody
    public String soleGoodsType(GoodsTypePojo goodsTypePojo){
        List<GoodsTypePojo> goodsTypePojos = goodsService.queryGoodsTypes(goodsTypePojo);
        if(goodsTypePojos!=null&&goodsTypePojos.size()>0){
            return "0000";
        }else{
            return "0001";
        }
    }

    //修改类型
    @RequestMapping("updType")
    @ResponseBody
    public ResponseMsg updType(MultipartFile myfile,GoodsTypePojo goodsTypePojo,HttpServletRequest request){
        ResponseMsg responseMsg = new ResponseMsg();
        if(myfile!=null&&myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""){
            String typeImage = request.getServletContext().getRealPath("typeImage");
            File file = new File(typeImage);
            if(!file.exists()){
                file.mkdir();
            }
            typeImage = typeImage + File.separator+UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            try {
                FileOutputStream fos = new FileOutputStream(typeImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                fos.close();
                is.close();
                goodsTypePojo.setTimage(File.separator+request.getContextPath().substring(1)+File.separator+typeImage.substring(typeImage.lastIndexOf(File.separator,typeImage.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean b = goodsService.updGoodsType(goodsTypePojo);
        if(b){
            responseMsg.setResult("0000");
        }else{
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("修改失败");
        }
        return responseMsg;
    }

    //添加类型
    @RequestMapping("addType")
    public String addType(GoodsTypePojo goodsTypePojo){
        boolean b = goodsService.addGoodsType(goodsTypePojo);
        if(b){
            return "redirect:typeList";
        }else{
            return "error";
        }
    }

    //删除类型
    @RequestMapping("delType")
    @ResponseBody
    public ResponseMsg delType(String tid){
        ResponseMsg rm = new ResponseMsg();
        String[] tids;
        if (tid.contains(",")){
            tids = tid.split(",");
        }else{
            tids = new String[]{tid};
        }
        boolean b = goodsService.delGoodsType(tids);
        if(b){
            rm.setResult("0000");
            return rm;
        }else{
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
            return rm;
        }
    }

    //跳转到商品列表界面
    @RequestMapping("goodsList")
    public String goodsList(GoodsPojo goodsPojo,HttpServletRequest request){
        PageHelper.startPage(goodsPojo.getPageNum(),goodsPojo.getPageSize());
        List<GoodsPojo> goodsPojos = goodsService.queryGoodss(goodsPojo);
        PageInfo<GoodsPojo> goodsPojoPageInfo = new PageInfo<>(goodsPojos);
        request.setAttribute("gname",goodsPojo.getGname());
        request.setAttribute("pageInfo",goodsPojoPageInfo);
        return "goods-list";
    }

    //删除商品
    @RequestMapping("delGoods")
    @ResponseBody
    public ResponseMsg delGoods(String gid){
        ResponseMsg responseMsg = new ResponseMsg();
        String[] gids;
        if (gid.contains(",")){
            gids = gid.split(",");
        }else{
            gids = new String[]{gid};
        }
        boolean b = goodsService.delGoods(gids);
        if(b){
            responseMsg.setResult("0000");
            return responseMsg;
        }else{
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("删除失败");
            return responseMsg;
        }
    }

    //修改商品
    @RequestMapping("updGoods")
    @ResponseBody
    public ResponseMsg updGoods(MultipartFile myfile, GoodsPojo goodsPojo,HttpServletRequest request){
        ResponseMsg responseMsg = new ResponseMsg();
        if(myfile!=null&&myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""){
            String goodsImage = request.getServletContext().getRealPath("goodsImage");
            File file = new File(goodsImage);
            if(!file.exists()){
                file.mkdir();
            }
            goodsImage = goodsImage + File.separator+UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            try {
                FileOutputStream fos = new FileOutputStream(goodsImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                fos.close();
                is.close();
                goodsPojo.setGimage(File.separator+request.getContextPath().substring(1)+File.separator+goodsImage.substring(goodsImage.lastIndexOf(File.separator,goodsImage.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean b = goodsService.updGoods(goodsPojo);
        if(b){
            responseMsg.setResult("0000");
        }else{
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("修改失败");
        }
        return responseMsg;
    }

    //添加商品
    @RequestMapping("addGoods")
    @ResponseBody
    public ResponseMsg addGoods(MultipartFile myfile, GoodsPojo goodsPojo,HttpServletRequest request){
        ResponseMsg responseMsg = new ResponseMsg();
        if(myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""&&goodsPojo!=null&&!StringUtil.isEmpty(goodsPojo.getGname())){
            String goodsImage = request.getServletContext().getRealPath("goodsImage");
            File file = new File(goodsImage);
            if(!file.exists()){
                file.mkdir();
            }
            goodsImage = goodsImage + File.separator+UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            try {
                FileOutputStream fos = new FileOutputStream(goodsImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                fos.close();
                is.close();
                goodsPojo.setGimage(File.separator+request.getContextPath().substring(1)+File.separator+goodsImage.substring(goodsImage.lastIndexOf(File.separator,goodsImage.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean b = goodsService.addGoods(goodsPojo);
        if(b){
            responseMsg.setResult("0000");
        }else{
            responseMsg.setResult("0001");
            responseMsg.setResultMsg("修改失败");
        }
        return responseMsg;
    }

    //跳转到商品添加界面
    @RequestMapping("goodsAdd")
    public String goodsAdd(HttpServletRequest request){
        List<GoodsTypePojo> goodsTypePojos = goodsService.queryGoodsTypes(new GoodsTypePojo());
        request.setAttribute("goodsTypes",goodsTypePojos);
        return "goods-add";
    }

    //跳转到商品修改界面
    @RequestMapping("goodsEdit")
    public String goodsEdit(String gid,HttpServletRequest request){
        GoodsPojo goodsPojo = goodsService.queryGoodsById(gid);
        List<GoodsTypePojo> goodsTypePojos = goodsService.queryGoodsTypes(new GoodsTypePojo());
        request.setAttribute("goodsTypes",goodsTypePojos);
        request.setAttribute("goodsPojo",goodsPojo);
        return "goods-edit";
    }
}
